package cn.iocoder.yudao.module.book.controller.admin.homeworks;

import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.book.enums.ErrorCodeConstants;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.book.controller.admin.homeworks.vo.*;
import cn.iocoder.yudao.module.book.dal.dataobject.homeworks.HomeworksDO;
import cn.iocoder.yudao.module.book.service.homeworks.HomeworksService;

@Tag(name = "管理后台 - 作业")
@RestController
@RequestMapping("/book/homeworks")
@Validated
public class HomeworksController {

    @Resource
    private HomeworksService homeworksService;

    @PostMapping("/create")
    @Operation(summary = "创建班级作业")
    @ApiAccessLog(operateType = CREATE)
    public CommonResult<Integer> createHomeworks(@Valid @RequestBody HomeworksSaveReqVO createReqVO) {
        Integer homeworkId = homeworksService.createHomeworks(createReqVO);
        return success(homeworkId);
    }

    @PutMapping("/update")
    @Operation(summary = "更新作业")
    @PreAuthorize("@ss.hasPermission('book:homeworks:update')")
    public CommonResult<Boolean> updateHomeworks(@Valid @RequestBody HomeworksSaveReqVO updateReqVO) {
        homeworksService.updateHomeworks(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除作业")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('book:homeworks:delete')")
    public CommonResult<Boolean> deleteHomeworks(@RequestParam("id") Integer id) {
        homeworksService.deleteHomeworks(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得作业")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('book:homeworks:query')")
    public CommonResult<HomeworksRespVO> getHomeworks(@RequestParam("id") Integer id) {
        HomeworksDO homeworks = homeworksService.getHomeworks(id);
        return success(BeanUtils.toBean(homeworks, HomeworksRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得作业分页")
    @PreAuthorize("@ss.hasPermission('book:homeworks:query')")
    public CommonResult<PageResult<HomeworksRespVO>> getHomeworksPage(@Valid HomeworksPageReqVO pageReqVO) {
        PageResult<HomeworksDO> pageResult = homeworksService.getHomeworksPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, HomeworksRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出作业 Excel")
    @PreAuthorize("@ss.hasPermission('book:homeworks:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportHomeworksExcel(@Valid HomeworksPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<HomeworksDO> list = homeworksService.getHomeworksPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "作业.xls", "数据", HomeworksRespVO.class,
                        BeanUtils.toBean(list, HomeworksRespVO.class));
    }

}